/*******************************************************************************
 * Copyright (c) 2012, 2013 XML-Print Project.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     XML-Print Project - initial API and implementation
 ******************************************************************************/
package info.xmlprint.action;

import info.xmlprint.core.project.PrintProject;
import info.xmlprint.run.Constants;
import info.xmlprint.run.RuntimeUtil;
import info.xmlprint.util.FileUtil;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.ui.MultiPartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;
import org.eclipse.ui.wizards.newresource.BasicNewProjectResourceWizard;

public class NewProject extends AbstractHandler {

	private class NewWizard extends BasicNewProjectResourceWizard {
		public void addPages() {
			super.addPages();
		}

		public boolean performFinish() {
			if (super.performFinish() == false) {
				return false;
			}

			IProject newProject = getNewProject();
			if (newProject == null) {
				return false;
			}

			String[] folders = new String[] { Constants.CONTENT_INPUT_FOLDER,
					Constants.SETTING_OUTPUT_FOLDER,
					Constants.FO_OUTPUT_FOLDER, Constants.PDF_OUTPUT_FOLDER };

			if (FileUtil.createFolder(newProject, folders)) {
				IFolder contentFolder = newProject.getFolder(new Path(
						Constants.CONTENT_INPUT_FOLDER));

				IFile file = contentFolder.getFile(newProject.getName()
						+ ".xml");//$NON-NLS-1$
				if (!file.exists()) {
					String xmlstr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
							+ "<content>content hier</content>";//$NON-NLS-1$ 
					try {
						InputStream source = new ByteArrayInputStream(
								xmlstr.getBytes());
						file.create(source, IResource.NONE, null);
						source.close();
					} catch (CoreException | IOException e) {
						e.printStackTrace();
						return false;
					}
				}

				try {
					IDE.openEditors(PlatformUI.getWorkbench()
							.getActiveWorkbenchWindow().getActivePage(),
							new IFile[] { file });
				} catch (MultiPartInitException e) {
					e.printStackTrace();
					return false;
				}
			}
			RuntimeUtil rt = RuntimeUtil.getInstance();
			PrintProject pProject = new PrintProject(newProject);
			rt.register(pProject);
			rt.setCurrentPrintProject(pProject);
			return true;
		}
	}

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		NewWizard wizard = new NewWizard();
		wizard.init(PlatformUI.getWorkbench(), new StructuredSelection());
		WizardDialog dialog = new WizardDialog(null, wizard);
		dialog.open();
		return null;
	}

}
